Summit.Lib.JS.load('Data.Jax');

Summit.UI.Widget.Jax_Table = function (table)
{
	var self = this;
	this.feed = false;
	this.action = false

	this.masterId = 'action_group_ID';
	this.table = table;

	this.cols = new Array();
	this.varPTs = new Array();

	this.xvars = new Array();
	this.page = window.location.hash ? window.location.hash.substr(1) : 1;
	this.forcePage = window.location.hash ? true : false;
	this.per_page = 25;

	this.class_1 = 'row1';
	this.class_2 = 'row2';

	this.color1 = '#272727';
	this.color2 = '#303030';

	this.loader = false;
	
	table.jaxTable = this;
	
	this.createLoader = function()
	{
		var loader = {el:{}};
		
		loader.el.row = new Element('tr', {'class':'row3'})
		loader.el.col = new Element('td', {'colspan':self.cols.length,'align':'center'}).injectInside(loader.el.row);
		loader.el.text = new Element('h2', {'styles':{'color':'white','margin':'0'}}).setText('Loading...').injectInside(loader.el.col);
		loader.el.image = new Element('img', {'src':'/lib/img/barLoader.gif'}).injectInside(loader.el.col);
		
		loader.fx = new Fx.Styles(loader.el.col, {duration: 1000,wait: false,transition: Fx.Transitions.Quad.easeOut});
		
		loader.show = function()
		{
			var tBody = self.table.tBodies[0];
			loader.el.col.style.height = tBody.offsetHeight;
			tBody.empty();
			loader.el.row.injectInside(tBody);
			loader.fx.start({'height': [tBody.offsetHeight,0]})
		}
		
		loader.hide = function()
		{
			var tBody = self.table.tBodies[0];
			tBody.removeChild(loader.el.row);
		}
		
		return loader;
	}

	this.addPassThru = function(varName,varFunction)
	{
		this.varPTs[varName]=varFunction;
	}

	this.removePassThru = function(varName)
	{
		delete this.varPTs[varName];
	}

	this.recolorBody=function(table)
	{
		var tBody = self.table.tBodies[0];
		var y = 0;
		
		for(var i=0; i<tBody.rows.length; i++)
		{
			var row = tBody.rows[i];
			if(row.className!='row3')
			{
				var newColor = y++%2?self.color1:self.color2;
				
				if(row.style.backgroundColor!=newColor)
				row.fx.start({
					backgroundColor : newColor
				})
			}
		}
	}


	this.removeRow = function(row)
	{
		var row=row;
		while(row.tagName != 'TR')
		row = row.parentNode

		var table = row.parentNode
		while(table.tagName != 'TABLE')
		table = table.parentNode

		

		row.fx.start({
		'opacity':0
		}).chain(function(){
			row.style.height=row.offsetHeight;
			row.empty();
			row.fx.start({'height': [40,0]});
		}).chain(function(){
			row.remove();
			self.recolorBody(self.table);
		});
	}


	this.addCol = function(col)
	{
		self.cols.push(col)
	}

	this.addVar = function(varName,varValue)
	{
		for(var i=0; i<self.xvars.length; i++)
		{
			if(self.xvars[i].name==varName)
			{
				self.xvars[i].value=varValue;
				return true;
			}
		}

		var newVar = {name:varName,value:varValue}
		self.xvars.push(newVar)
		return true;
	}

	this.footer = function(searchInfo,count)
	{
		var tr = new Element('tr', {
		'class':'row3'
		});

		var td = new Element('td', {
			colSpan:self.cols.length
		});

		var miniTable = new Element('table', {
			styles: {
				width:'100%'
			}
		});

		var miniTr = new Element('tr', {
			styles: {

			}
		});

		var miniTdl = new Element('td', {
			styles: {
				width:'25%',
				border:'none',
				textAlgin:'left'
			}
		});

		var miniTdm = new Element('td', {
			styles: {
				width:'50%',
				border:'none',
				textAlign:'center',
				fontWeight:'900'
			},

			'class' : 'searchResults'
		});

		var miniTdr = new Element('td', {
			styles: {
				width:'25%',
				border:'none',
				textAlign:'right'
			}
		});

		var prevB = new Element('b', {
			styles: {
				cursor:'pointer',
				display:self.page==1 ? 'none' : ''
			},
			events: {
				click : function() {
					if(self.page>1)
					{
						self.page--;
						self.loadData();
					}
				}
			}
		});

		var prevImg = new Element('img', {
			src:'/lib/img/icons/mini/arrow_left.gif',
			styles: {
				display:self.page==1 ? 'none' : '',
				verticalAlign : 'text-bottom'
			}
		});

		var nextB = new Element('b', {
			styles: {
				cursor:'pointer',
				display:self.page==searchInfo.getAttribute('Pages') || parseInt(searchInfo.getAttribute('Pages')) == 0 ? 'none' : ''
			},
			events: {
				click : function() {
					if(self.page<searchInfo.getAttribute('Pages'))
					{
						self.page++;
						self.loadData();
					}
				}
			}
		});

		var nextImg = new Element('img', {
			src:'/lib/img/icons/mini/arrow_right.gif',
			styles: {
				display:self.page==searchInfo.getAttribute('Pages') || parseInt(searchInfo.getAttribute('Pages')) == 0 ? 'none' : '',
				verticalAlign : 'text-bottom'
			}
		});

		prevB.adopt(prevImg);
		prevB.appendText(' Previous Page');

		nextB.appendText(' Next Page');
		nextB.adopt(nextImg);

		miniTdl.adopt(prevB);
		miniTdr.adopt(nextB);

		var start = (self.per_page*(self.page-1))+1;
		var end = start + count - 1;

		miniTdm.innerHTML = "Page " + searchInfo.getAttribute('Page') + "/" + searchInfo.getAttribute('Pages');
		miniTdm.innerHTML += "&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;&nbsp;&nbsp;&nbsp; Viewing Results " + start + "-" + end;
		miniTdm.innerHTML += "&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;&nbsp;&nbsp;&nbsp; " + searchInfo.getAttribute('COUNT') + " Total";




		tr.adopt(td);
		td.adopt(miniTable);
		miniTable.adopt(miniTr);
		miniTr.adopt([miniTdl,miniTdm,miniTdr]);


		return tr;
	}

	this.parse = function(col,data)
	{
		col.runAgain=false;
		col.timesRan++;


		for(var j=0; j<data.length; j++)
		{
			varName = data[j].name;
			varValue = data[j].value;
			varValue = self.varPTs[varName] ? self.varPTs[varName](varValue) : varValue;


			col.value = col.value.replace('%'+varName+'%',varValue)

		}

		return col;
	}


	this.createRow = function(data)
	{
		var tr = new Element('tr');

		for(var i=0; i<self.cols.length; i++)
		{
			var col = {value:self.cols[i],runAgain:true}

			while(col.runAgain==true)
			col = self.parse(col,data)

			var newTd = new Element('td')
			newTd.setHTML(col.value);
			tr.adopt(newTd)
		}
		
		tr.fx = new Fx.Styles(tr, {
			duration: 350,
			wait: false,
			transition: Fx.Transitions.Quad.easeOut
		});

		tr.removeRow = function()
		{
			self.removeRow(this)
		}
		
		tr.jaxTable = self;

		return tr;
	}

	this.addRows=function(table,results,index,searchInfo)
	{
		if(!results[index])
		{
			var footer = self.footer(searchInfo,index);
			table.adopt(footer);
			table.style.height='';
			return true;
		}

		var result = results[index];
		var row = self.createRow(result.attributes);
		row.className = 'row';
		row.style.backgroundColor = index % 2 ? self.color1 : self.color2;
		table.adopt(row);

		self.addRows(table,results,index+1,searchInfo);




	}

	this.loadData = function(newPage)
	{
		var ckpre = self.feed+self.action+'JxTbl_';
		
		if(!self.loader)
		self.loader = new self.createLoader();
		
		self.loader.show();

		if(newPage && !self.forcePage)
		self.page=newPage;
		
		self.forcePage=false;

		var Qjax = new Summit.Data.Jax();
		Qjax.feed = self.feed;
		Qjax.action = self.action;

		Qjax.Var('page',self.page);
		Qjax.Var('per_page',self.per_page);

		for(var i=0; i<self.xvars.length; i++)
		{
			var xVar = self.xvars[i];

			if( (typeof xVar.value == 'string') && xVar.value.length>0)
			Qjax.Var(xVar.name,xVar.value);
			else if ((typeof xVar.value == 'number') && xVar.value>=1)
			Qjax.Var(xVar.name,xVar.value);
			else if ((typeof xVar.value == 'boolean'))
			Qjax.Var(xVar.name,xVar.value?'1':'0');
		}

		Qjax.handler=function(feed)
		{
			var tBody = self.table.tBodies[0];
			var results = feed.getElementsByTagName('results').item(0).childNodes;
			var count = results.length;
			var bodyHeight = 23*(count+1);

			self.loader.fx.start({'height': [self.loader.el.col.offsetHeight,bodyHeight]
			}).chain(function()
			{
				self.loader.hide();
				self.addRows(tBody,results,0,feed.getElementsByTagName('search').item(0));
			}) 
		}
		
		window.location.hash=self.page;
		
		Qjax.Call();
	}
}